package org.pmt.first.kms.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.unit.dp
import coil3.ImageLoader
import coil3.compose.AsyncImage
import coil3.compose.LocalPlatformContext
import coil3.util.Logger
import io.ktor.client.plugins.logging.LogLevel
import org.jetbrains.compose.ui.tooling.preview.Preview
import org.pmt.first.kms.data.entity.NewsHotItem
import org.pmt.first.kms.data.entity.NewsItem
import org.pmt.first.resource.IconRes

/**
 * Author: pengmutian
 * Date: 2025/3/4 11:06
 * Description: Itmes
 */
@Composable
@Preview
fun NewsListItem(news: NewsItem, onItemClick: () -> Unit) {
    Card(modifier = Modifier
        .fillMaxWidth()
        .padding(8.dp)) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp)
                .clickable { onItemClick() },
            verticalAlignment = Alignment.Top
        ) {
            Image(
                painter = rememberVectorPainter(IconRes.Favorite),
                contentDescription = null,
                modifier = Modifier
                    .size(100.dp)
                    .clip(CircleShape)
            )
            Spacer(modifier = Modifier.width(8.dp))
            Column {
                Text(text = news.title, style = MaterialTheme.typography.titleMedium)
                Spacer(modifier = Modifier.height(4.dp))
                Text(text = news.digest, style = MaterialTheme.typography.bodyMedium)
            }
        }
    }
}

class CustomLogger(override var minLevel: Logger.Level) : Logger {

    override fun log(tag: String, level: Logger.Level, message: String?, throwable: Throwable?) {
        when (level) {
            Logger.Level.Debug -> println("DEBUG: $message")
            Logger.Level.Info -> println("INFO: $message")
            Logger.Level.Warn -> println("WARNING: $message")
            Logger.Level.Error -> println("ERROR: $message")
            else -> println("UNKNOWN: $message")
        }
        throwable?.printStackTrace()
    }
}

@Composable
@Preview
fun NewsHotItem(news: NewsHotItem, onItemClick: () -> Unit) {
    Card(modifier = Modifier
        .fillMaxWidth()
        .padding(8.dp)) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp)
                .clickable { onItemClick() },
            verticalAlignment = Alignment.Top
        ) {
            AsyncImage(
                //model = "https://images.pexels.com/photos/30961748/pexels-photo-30961748.jpeg?auto=compress&cs=tinysrgb&w=1200",
                model = news.picUrl,
                contentDescription = null,
                imageLoader = ImageLoader.Builder(LocalPlatformContext.current)
                    .logger(CustomLogger(Logger.Level.Debug))
                    .build(),
                modifier = Modifier.size(100.dp).clip(CircleShape)
            )
            Spacer(modifier = Modifier.width(8.dp))
            Column {
                Text(text = news.title, style = MaterialTheme.typography.titleMedium)
                Spacer(modifier = Modifier.height(4.dp))
                Text(text = news.description, style = MaterialTheme.typography.bodyMedium)
            }
        }
    }
}